﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using SSG.Model;
using SSG.Tools;

namespace SSG.UnitTests
{
    /// <summary>
    /// Conversions of degrees: Min / Seconds to decimal, vice versa
    /// </summary>
    [TestClass]
    public class UnitTestDegreeConversions
    {
        public const double Delta = 0.00001;

        /// <summary>
        /// Testing conversion from PMDG format
        /// </summary>
        [TestMethod]
        public void FixCoordinatesFromPmdg()
        {
            // Remarks: PMDG Format
            // From EDDN.txt FX AKINI LATLON N 48 44.98332 E 12 7.45002
            // => DEG / MIN / Decimals not Seconds!
            Fix f = Fix.FixFromNavigraphPmdgFormat("FIX RW10 LATLON N 49 30.033 E 11 3.55014");
            Assert.AreEqual(49.50055, f.Latitude, Delta, "Latitude not matching for " + f.Name);

            // VORTAC ERL: FX ERL LATLON N 49 39.32484 E 11 9.04866
            // ERLANGEN  ERL  VORD 49.655414  11.150811 114.90H (PMDG navaid file)
            f = Fix.FixFromNavigraphPmdgFormat("FIX ERL LATLON N 49 39.32484 E 11 9.04866");
            Assert.AreEqual(49.655414, f.Latitude, Delta, "Latitude not matching for " + f.Name);
            Assert.AreEqual(11.150811, f.Longitude, Delta, "Latitude not matching for " + f.Name);
            
            // ERL values but negativ
            f = Fix.FixFromNavigraphPmdgFormat("FIX ERL-Inverse LATLON S 49 39.32484 W 11 9.04866");
            Assert.AreEqual(-49.655414, f.Latitude, Delta, "Latitude not matching for " + f.Name);
            Assert.AreEqual(-11.150811, f.Longitude, Delta, "Latitude not matching for " + f.Name);
            
            // NUB VOR: NURNBERG  NUB  VORD 49.502942  11.034775 115.75H
            f = Fix.FixFromNavigraphPmdgFormat("FIX NUB LATLON N 49 30.17652 E 11 2.0865");
            Assert.AreEqual(49.502942, f.Latitude, Delta, "Latitude not matching for " + f.Name);
            Assert.AreEqual(11.034775, f.Longitude, Delta, "Latitude not matching for " + f.Name);
        }

        /// <summary>
        /// Testing conversion from PMDG format
        /// </summary>
        [TestMethod]
        public void FixCoordinatesToMinSec()
        {
            int d, m, s, ms;
            Fix f = new Fix("TestFix", this.GetClassname(), "UnitTest", 49.50833333, 11.15083333);
            Fix.LatLngDegToDegMinSec(f.Latitude, false, out d, out m, out s, out ms);
            Assert.AreEqual(49, d, "Degrees not matching");
            Assert.AreEqual(30, m, "Minutes not matching");
            Assert.AreEqual(30, s, "Seconds not matching");

            f = Fix.FixFromNavigraphPmdgFormat("FIX NUB LATLON S 49 30.17652 E 11 2.0865");
            double lat1 = f.Latitude;
            string scnStr = Coordinates.LatLngDegToDegMinSecStr(f.Latitude);
            double lat2 = Coordinates.LatLngDegMinSecMsStrToDecimalDeg(scnStr);
            Assert.AreEqual(lat1, lat2, Delta, "Mismatch for Lat/Lng string " + scnStr);
        }
    }
}